package controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.9.6 下午 23:34
 *
 * 一个公共的Servlet
 * 业务:实现自己的业务功能
 *
 */
public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            Class clazz = this.getClass();//获取当前的字节码文件
            Object obj = clazz.newInstance();//获取当前的类实例

            String methodName = request.getParameter("methodName");
            if(methodName == null){
                methodName = "index";//访问首页
            }

            Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            String invoke = (String) method.invoke(obj, request, response);

            if(invoke != null){
                request.getRequestDispatcher(invoke).forward(request,response);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
            }
        }

        public String index(HttpServletRequest request,HttpServletResponse response){
            return null;
        }


   }
